#!/usr/bin/python3
# -*- coding:utf-8 -*-
"""
@File: EnvConfig.py
@Author: lijk34925 
@Date: 2024/9/13 11:13
@desc: 请补充本模块功能说明
"""

from copy import deepcopy
from functools import lru_cache

'''
若传入env,且非类假,以传入的为准
若传入env为类假,则取执行机环境,若非执行机执行,去nkConfig下的默认配置
'''


class EnvConfig(object):
    config = None

    def __init__(self, env=None, config=None, **kwargs):
        self._env = env
        self.config = config
        self.__class__.config = deepcopy(config)

    @property
    @lru_cache(None)
    def env(self):
        if not self._env:
            return self.__class__.config['env']
        else:
            return self._env

    @property
    @lru_cache(None)
    def title(self):
        return self.config[self.env]['title']

    @property
    @lru_cache(None)
    def dbType(self):
        return self.__class__.config[self.env]['databases']['type']

    @property
    @lru_cache(None)
    def url(self):
        return self.__class__.config[self.env]['system']['url']

    @property
    @lru_cache(None)
    def user(self):
        return self.__class__.config[self.env]['system']['username']

    @property
    @lru_cache(None)
    def pwd(self):
        return self.__class__.config[self.env]['system']['password']

    @property
    @lru_cache(None)
    def ip(self):
        return self.config[self.env]['machine']['ip']

    @property
    @lru_cache(None)
    def systemConfig(self):
        return deepcopy(self.__class__.config[self.env]['system'])

    @property
    @lru_cache(None)
    def seeConfig(self):
        return deepcopy(self.__class__.config[self.env]['see'])

    @property
    @lru_cache(None)
    def machineConfig(self):
        return deepcopy(self.__class__.config[self.env]['machine'])

    @property
    @lru_cache(None)
    def redisConfig(self):
        return deepcopy(self.__class__.config[self.env]['redis'])

    @property
    @lru_cache(None)
    def dbConfig(self):
        dbconfig = deepcopy(self.__class__.config[self.env]['databases'])
        dbconfig.pop('type')
        return dbconfig

    def __getattr__(self, item):
        if item in self.__class__.config[self.env]:
            return self.__class__.config[self.env][item]
        else:
            raise AttributeError(f"type object '{self.__class__}' has no attribute '{item}'")


if __name__ == '__main__':
    env=EnvConfig('mysql')
    print(env.dbConfig)
    # print(env.password)
    # print(env.pwd)
    # print(env.see)
    print(env.linux_pwd)
    print(env.databases)
    print(env.see)